package com.ruoyi.web.controller.devops;

import cn.hutool.json.JSONUtil;
import com.alibaba.druid.support.json.JSONUtils;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.devops.domain.*;
import com.ruoyi.devops.domain.vo.HisReportVo;
import com.ruoyi.devops.domain.vo.InspectionAdgVO;
import com.ruoyi.devops.service.impl.InspectionAdgService;
import com.ruoyi.devops.service.impl.InspectionReportService;
import com.ruoyi.devops.service.impl.InspectionTaskService;
import com.ruoyi.devops.service.impl.InspectionTemplateService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

import static com.ruoyi.common.core.domain.AjaxResult.success;

@RestController
@RequestMapping("/inspectionController")
public class InspectionController {

    @Autowired
    InspectionTemplateService inspectionTemplateService;

    @Autowired
    InspectionTaskService inspectionTaskService;
    @Autowired
    InspectionReportService inspectionReportService;

    @Autowired
    InspectionAdgService inspectionAdgService;

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        binder.setDisallowedFields(new String[]{"admin"});
    }
    // 巡检模块管理
    @Log(title = "查询巡检模块", businessType = BusinessType.QUERY)
    @PostMapping("queryInspectionTemplate")
    public AjaxResult queryInspectionTemplate(@RequestBody Map<String,String> map) {
        return  inspectionTemplateService.queryInspectionTemplate(map);
    }
    //双活工具  查询
    @Log(title = "查询双活工具", businessType = BusinessType.QUERY)
    @PostMapping("queryInspectionAdgTemplate")
    public AjaxResult queryInspectionAdgTemplate(@RequestBody InspectionAdgVO inspectionAdg) {
        List<InspectionAdg> list = inspectionAdgService.queryInspectionAdgTemplate(inspectionAdg);
        return success(list);
    }

    @Log(title = "查询巡检任务管理", businessType = BusinessType.QUERY)
    @PostMapping("queryInspectionTaskTemplate")
    public AjaxResult queryInspectionTaskTemplate(@RequestBody Map<String,String> map) {
        return  inspectionTaskService.queryInspectionTaskTemplate(map);

    }

    //根据id查找
    @PostMapping("queryInspectionTemplateById")
    public AjaxResult queryInspectionTemplateById(@RequestBody InspectionTemplate inspectionTemplate) {
        InspectionTemplate list = inspectionTemplateService.queryInspectionTemplateById(inspectionTemplate);
        return success(list);
    }
    //根据id查找任务
    @PostMapping("queryInspectionTaskTemplateById")
    public AjaxResult queryInspectionTaskTemplateById(@RequestBody InspectionTask inspectionTask) {
        InspectionTask list = inspectionTaskService.queryInspectionTaskTemplateById(inspectionTask);
        return success(list);
    }

    ////双活工具  根据id查询
    @PostMapping("queryInspectionAdgTemplateById")
    public AjaxResult queryInspectionAdgTemplateById(@RequestBody InspectionAdg inspectionAdg) {
        InspectionAdg list = inspectionAdgService.queryInspectionAdgTemplateById(inspectionAdg);
        return success(list);
    }

    //添加管理
    @Log(title = "新增巡检模板管理", businessType = BusinessType.INSERT)
    @PostMapping("addInspectionTemplate")
    public String addInspectionTemplate(@RequestBody InspectionTemplate inspectionTemplate) {
        boolean save = inspectionTemplateService.save(inspectionTemplate);
        if(save){
            return "新增成功";

        }else {
            return "新增失败";
        }
    }


    //添加任务
    @Log(title = "新增巡检任务管理", businessType = BusinessType.INSERT)
    @PostMapping("addInspectionTaskTemplate")
    public String addInspectionTemplate(@RequestBody InspectionTask inspectionTask) {
        Calendar calendar= Calendar.getInstance();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
        //System.out.println(dateFormat.format(calendar.getTime()));
        inspectionTask.setCreateTime(dateFormat.format(calendar.getTime()));
        inspectionTask.setTaskUser("管理员");
        inspectionTask.setStatus("0");
        boolean save = inspectionTaskService.save(inspectionTask);
        if(save){
            return "新增成功";

        }else {
            return "新增失败";
        }
    }
    ////双活工具  新增
    @Log(title = "新增双活工具", businessType = BusinessType.INSERT)
    @PostMapping("addInspectionAdgTemplate")
    public String addInspectionAdgTemplate(@RequestBody InspectionAdg inspectionAdg) {

        //System.out.println(dateFormat.format(calendar.getTime()));
        inspectionAdg.setFlag("1");
        inspectionAdg.setStatus("0");

        boolean save = inspectionAdgService.save(inspectionAdg);
        if(save){
            return "新增成功";

        }else {
            return "新增失败";
        }
    }
    //删除管理
    @Log(title = "删除巡检模板管理", businessType = BusinessType.DELETE)
    @PostMapping("delInspectionTemplate")
    public AjaxResult delInspectionTemplate(@RequestBody InspectionTemplate inspectionTemplate) {
        int list = inspectionTemplateService.delInspectionTemplate(inspectionTemplate);
        return success(list);
    }
    @Log(title = "删除巡检任务管理", businessType = BusinessType.DELETE)
    @PostMapping("delInspectionTaskTemplate")
    public AjaxResult delInspectionTemplate(@RequestBody InspectionTask inspectionTask) {
        int list = inspectionTaskService.delInspectionTaskTemplate(inspectionTask);
        return success(list);
    }

    //双活  删除
    @Log(title = "删除双活工具", businessType = BusinessType.DELETE)
    @PostMapping("delInspectionAdgTemplate")
    public AjaxResult delInspectionAdgTemplate(@RequestBody InspectionAdg inspectionAdg) {
        int list = inspectionAdgService.delInspectionAdgTemplate(inspectionAdg);
        return success(list);
    }
    //修改管理
    @Log(title = "修改巡检任务", businessType = BusinessType.UPDATE)
    @PostMapping("updInspectionTemplate")
    public AjaxResult updInspectionTemplate(@RequestBody InspectionTemplate inspectionTemplate) {
        int list = inspectionTemplateService.updInspectionTemplate(inspectionTemplate);
        return success(list);
    }


    //
    @PostMapping("scInspectionTaskTemplate")
    public AjaxResult scInspectionTaskTemplate(@RequestBody InspectionTask inspectionTask) {
        int list = inspectionTaskService.scInspectionTaskTemplate(inspectionTask);
        return success(list);
    }
////双活工具  修改
    @Log(title = "修改双活工具", businessType = BusinessType.UPDATE)
    @PostMapping("scInspectionAdgTemplate")
    public AjaxResult scInspectionAdgTemplate(@RequestBody InspectionAdg inspectionTask) {
        int list = inspectionAdgService.scInspectionAdgTemplate(inspectionTask);
    return success(list);
    }
    //双活工具   修改模式

    @Log(title = "修改双活工具", businessType = BusinessType.UPDATE)
    @PostMapping("scInspectionAdgTemplatemodel")
    public AjaxResult scInspectionAdgTemplatemodel(@RequestBody InspectionAdg inspectionTask) {
        int list = inspectionAdgService.scInspectionAdgTemplatemodel(inspectionTask);
        return success(list);
    }


    //双活  切换
    @PostMapping("scInspectionQhTemplate")
    public AjaxResult scInspectionQhTemplate(@RequestBody InspectionAdg inspectionTask) {
        int list = inspectionAdgService.scInspectionQhTemplate(inspectionTask);
        return success(list);
    }
    //修改任务
    @Log(title = "修改巡检任务", businessType = BusinessType.UPDATE)
    @PostMapping("updInspectionTaskTemplate")
    public AjaxResult updInspectionTemplate(@RequestBody InspectionTask inspectionTask) {
        int list = inspectionTaskService.updInspectionTaskTemplate(inspectionTask);
        return success(list);
    }

    //镜像复制
    @PostMapping("copyInspectionTaskTemplate")
    public AjaxResult copyInspectionTaskTemplate(@RequestBody InspectionTask inspectionTask) {
        inspectionTaskService.copyInspectionTaskTemplate(inspectionTask);
        return success("复制成功");
    }
    @Log(title = "查询巡检报告", businessType = BusinessType.QUERY)
    @PostMapping("queryInspectionReportTemplate")
    public AjaxResult queryInspectionReportTemplate(@RequestBody InspectionReport inspectionReport) {
        List<InspectionReport> list = inspectionReportService.queryInspectionReportTemplate(inspectionReport);
        return success(list);
    }

    // 导出历史报告Excel
    @PostMapping("exportReports")
    public void exportHisReports(HttpServletResponse response, InspectionTask inspectionTask) {
        List<InspectionTask> list = new ArrayList();
        InspectionTask list2 = inspectionTaskService.queryInspectionTaskTemplateById(inspectionTask);
        list.add(list2);
        ExcelUtil<InspectionTask> util = new ExcelUtil<>(InspectionTask.class);
        util.exportExcel(response, list, "任务报告");
    }

}
